import axios from 'axios'
import { ElMessage } from 'element-plus'

const api = axios.create({
  baseURL: '/api',
  timeout: 10000
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    if (error.response?.status === 401) {
      localStorage.removeItem('token')
      window.location.href = '/login'
    }
    return Promise.reject(error.response?.data || error)
  }
)

// 用户相关
export const login = (data) => api.post('/auth/login', data)
export const getUserInfo = () => api.get('/auth/profile')

// ERP相关
export const getInventoryList = (params) => api.get('/erp/inventory', { params })
export const createInventory = (data) => api.post('/erp/inventory', data)
export const updateInventory = (id, data) => api.put(`/erp/inventory/${id}`, data)
export const deleteInventory = (id) => api.delete(`/erp/inventory/${id}`)

export const getPurchaseList = (params) => api.get('/erp/purchase', { params })
export const createPurchase = (data) => api.post('/erp/purchase', data)
export const updatePurchase = (id, data) => api.put(`/erp/purchase/${id}`, data)

export const getSalesList = (params) => api.get('/erp/sales', { params })
export const createSales = (data) => api.post('/erp/sales', data)
export const updateSales = (id, data) => api.put(`/erp/sales/${id}`, data)

// MES相关
export const getWorkOrderList = (params) => api.get('/mes/workorder', { params })
export const createWorkOrder = (data) => api.post('/mes/workorder', data)
export const updateWorkOrder = (id, data) => api.put(`/mes/workorder/${id}`, data)
export const startWorkOrder = (id) => api.post(`/mes/workorder/${id}/start`)
export const completeWorkOrder = (id, data) => api.post(`/mes/workorder/${id}/complete`, data)

export const getProcessList = (params) => api.get('/mes/process', { params })
export const createProcess = (data) => api.post('/mes/process', data)
export const updateProcess = (id, data) => api.put(`/mes/process/${id}`, data)

export const getEquipmentList = (params) => api.get('/mes/equipment', { params })
export const createEquipment = (data) => api.post('/mes/equipment', data)
export const updateEquipment = (id, data) => api.put(`/mes/equipment/${id}`, data)

export const getQualityList = (params) => api.get('/mes/quality', { params })
export const createQuality = (data) => api.post('/mes/quality', data)

// 生产管理相关
export const getScheduleList = (params) => api.get('/production/schedule', { params })
export const createSchedule = (data) => api.post('/production/schedule', data)
export const updateSchedule = (id, data) => api.put(`/production/schedule/${id}`, data)

export const getKanbanData = (params) => api.get('/production/kanban', { params })
export const getMaterialList = (params) => api.get('/production/material', { params })
export const createMaterialRequest = (data) => api.post('/production/material', data)

// 实时数据
export const getRealtimeData = () => api.get('/realtime/data')
